package com.demo.service.system.impl;

import java.util.List;

import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.demo.dto.Pager;
import com.demo.mapper.system.${entityName}Mapper;
import com.demo.model.system.${entityName};
import com.demo.model.system.${entityName}Example;
import com.demo.service.system.${entityName}Service;

@Service
public class ${entityName}ServiceImpl implements ${entityName}Service {

	@Autowired
	${entityName}Mapper mapper;
	
	@Override
	@Transactional
	public int add(${entityName} record) {
		if (null == record) {
			return 0;
		}
		return mapper.insert(record);
	}

	@Override
	@Transactional
	public int update(${entityName} record) {
		if (null == record || null == record.getId()) {
			return 0;
		}
		return mapper.updateByPrimaryKeySelective(record);
	}

	@Override
	@Transactional
	public int enable(List<Integer> ids) {
		if (ids == null || ids.size()==0) {
			return 0;
		}
		${entityName}Example example = new ${entityName}Example();
		example.or().andIdIn(ids);
		${entityName} record = new ${entityName}();
		record.setStoreStatus(Boolean.TRUE);
		return mapper.updateByExampleSelective(record, example);
	}

	@Override
	@Transactional
	public int disable(List<Integer> ids) {
		if (ids == null || ids.size()==0) {
			return 0;
		}		
		${entityName}Example example = new ${entityName}Example();
		example.or().andIdIn(ids);
		${entityName} record = new ${entityName}();
		record.setStoreStatus(Boolean.FALSE);
		return mapper.updateByExampleSelective(record, example);
	}

	@Override
	public ${entityName} getById(Integer id) {
		return mapper.selectByPrimaryKey(id);
	}

	@Override
	public List<${entityName}> findByCondition(${entityName}Example example) {
		return mapper.selectByExample(example);
	}

	@Override
	public int getCount(${entityName}Example example) {
		return mapper.countByExample(example);
	}

	@Override
	public Pager<${entityName}> findByPage(${entityName}Example example, Pager<${entityName}> pager) {
		if (pager == null) {
			return pager;
		}
		RowBounds rowBounds = new RowBounds(pager.getStart(), pager.getPageSize());
		pager.setData(mapper.selectByExample(example, rowBounds));
		pager.setTotalRow(mapper.countByExample(example));
		return pager;
	}

}
